<template>
  <el-dialog :visible.sync="dialog" title="分配学管师" :close-on-click-modal="false" @open="openDialog" @close="closeDialog">
    <el-form ref="advisorForm" :model="advisorForm" :rules="rules" label-width="110px">
      <el-row>
        <el-col :span="12">
          <el-form-item label="学员姓名：" prop="name">{{ studentName }}</el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="学管老师：" prop="advisorId" v-permission="['ADMIN', 'STUDENT_ALL', 'STUDENT_ASSIGN']">
            <el-select v-model="advisorForm.advisorId" placeholder="请选择">
              <el-option
                v-for="item in advisors"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="text" @click="cancel">取消</el-button>
      <el-button :loading="loading" type="primary" @click="doSubmit">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { advisor, getAdvisorListOptions } from '@/api/advisor'
  export default {
    props: {
      studentId: {
        type: Number,
        required: true
      },
      studentName: {
        type: String,
        required: true
      }
    },
    data() {
      return {
        loading: false,
        dialog: false,
        advisors: [],
        advisorForm: {
          merchantStudentId: null,
          advisorId: null
        },
        rules: {
          merchantStudentId: [{ required: true, message: '请选择学员', trigger: 'blur' }],
          advisorId: [{ required: true, message: '请选择学管师', trigger: 'blur' }]
        }
      }
    },
    watch: {
      merchantStudentId() {
        this.advisorForm.merchantStudentId = this.studentId
      }
    },
    mounted() {
      
    },
    methods: {
      openDialog() {
        console.log('open advisor')
        getAdvisorListOptions().then(res => {
          this.advisors = res.data
        })
      },
      closeDialog() {
        console.log('close advisor')
      },
      cancel() {
        this.resetForm()
      },
      doSubmit() {
        this.advisorForm.merchantStudentId = this.studentId
        advisor(this.advisorForm).then(res => {
          this.dialog = false
          this.$parent.init()
        }).catch(err => {
          this.dialog = false
          console.log(err)
        })
      },
      resetForm() {
        this.dialog = false
        this.$refs['advisorForm'].resetFields()
      }
    }
  }

</script>
